﻿using System.ComponentModel;
using System;
namespace DeepEarth.Map.Core.Tiling
{
    /// <summary>
    /// A type converter for the map mode. Can convert from <c>string</c> to <c>MapMode</c>.
    /// </summary>
    /// <remarks>
    /// This type converter is used by XAML when the mode is parsed from the string.
    /// For example, the map mode is specified like: 
    /// <example>
    /// &lt;nearmap:NearMapTileOverlay Mode="PhotoMap" /&gt;
    /// </example>
    /// In this case the "PhotoMap" string has to be converter into a MapMode instance which has a "PhotoMap" 
    /// as an <see cref="MapMode.Identifier"/>.
    /// </remarks>
    public sealed class MapModeTypeConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string)) return true;
            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            var strValue = (string)value;
            return new StubMapMode(strValue);
        }

        private sealed class StubMapMode : MapMode
        {
            public StubMapMode(string identifier)
                : base("stubProvider", "stubKey", identifier, "Stub Map Mode, Need to be coerced: " + identifier)
            {
            }
        }
    }
}
